
<?

require_once ("application.list.inc");

global $_GLOBAL_DBH;
global $_OBJECT_IFACE_PANEL;

if (($task_timer = $_GLOBAL_DBH->query ("SELECT * FROM project_task_timer WHERE task_id={$_REQUEST["task_id"]} AND user_id={$_APP_SESSION_USER->id} AND date_end = ''")) && $_GLOBAL_DBH->num_rows ($task_timer)) {

	$task_timer = $_GLOBAL_DBH->fetch_object ($task_timer);
	$task_timer_action = "check_in";
} else {
	$task_timer = null;
	$task_timer_action = "check_out";
}

$task_timer_status = $fieldset_tasks->get_field ("status")->get_variable ("value") == "complete" ? "check_reopen" : "check_complete";

if (isset ($_REQUEST["fs_task_timer"])) {

	if (preg_match ("/in|complete/", $_REQUEST["fs_task_timer"]) && $task_timer) {

		$status = $_REQUEST["fs_task_timer"] == "in" ?
				"inactive" : "complete";
		$_GLOBAL_DBH->query ("UPDATE project_task_timer SET date_end=CURRENT_TIMESTAMP() WHERE id={$task_timer->id}");
		$_GLOBAL_DBH->query ("UPDATE project_tasks SET status='{$status}' WHERE id={$_REQUEST["task_id"]}");
		$fieldset_tasks->get_field ("status")->
				set_variable ("value", $status);

		if ($_REQUEST["fs_task_timer"] == "complete") {
			$task_timer_action = "check_none";
			$task_timer_status = "check_reopen";
		} else {
			$task_timer_action = "check_out";
			$task_timer_status = "check_complete";
		}
	} elseif ($_REQUEST["fs_task_timer"] == "out" && !$task_timer) {

		$_GLOBAL_DBH->query ("INSERT INTO project_task_timer VALUES (NULL, {$_REQUEST["task_id"]}, {$_APP_SESSION_USER->id}, CURRENT_TIMESTAMP(), '', NULL)");
		$_GLOBAL_DBH->query ("UPDATE project_tasks SET status='active' WHERE id={$_REQUEST["task_id"]}");
		$fieldset_tasks->get_field ("status")->
				set_variable ("value", "active");
		$task_timer_action = "check_in";
		$task_timer_status = "check_complete";
	} elseif ($_REQUEST["fs_task_timer"] == "complete") {

		$_GLOBAL_DBH->query ("UPDATE project_tasks SET status='complete', date_end=CURRENT_TIMESTAMP() WHERE id={$_REQUEST["task_id"]}");
		$fieldset_tasks->get_field ("status")->
				set_variable ("value", "complete");
		$task_timer_action = "check_none";
		$task_timer_status = "check_reopen";
	} elseif ($_REQUEST["fs_task_timer"] == "reopen") {

		$_GLOBAL_DBH->query ("UPDATE project_tasks SET status='inactive', date_end=CURRENT_TIMESTAMP() WHERE id={$_REQUEST["task_id"]}");
		$fieldset_tasks->get_field ("status")->
				set_variable ("value", "inactive");
		$task_timer_action = "check_out";
		$task_timer_status = "check_complete";
	}
}

$indexes = array (
	user_name=>array (label=>"User Name",order=>"DEFAULT",wrapper=>"vmc_icon_user"),
	date_begin=>array (label=>"Date Begin",order=>"DEFAULT",wrapper=>"vmc_icon_time_in"),
	date_end=>array (label=>"Date End",order=>"DEFAULT",wrapper=>"vmc_icon_time_out"),
	total_time=>array (label=>"Total Time",order=>"DEFAULT",wrapper=>"vmc_icon_total_time"),
);

$object_ctl = "/admin/projects/projects/fs_projects.php?fs_tab=tasks&project_id={$_REQUEST["project_id"]}&fs_tab_option=fs_tasks&fs_action=view&fs_tab_tasks=main&fs_tab_option_tasks=fs_timer&task_id={$_REQUEST["task_id"]}&";
$link_options = "&fs_tab=tasks&project_id={$_REQUEST["project_id"]}&fs_tab_option=fs_tasks&fs_action=view&fs_tab_tasks=main&task_id={$_REQUEST["task_id"]}";
$list_options = array ();
$check_out_task = array (
		label=>"Check Out Task",
		link=>"fs_projects.php?fs_tab=tasks&project_id={$_REQUEST["project_id"]}&fs_tab_option=fs_tasks&fs_action=view&fs_tab_tasks=main&task_id={$_REQUEST["task_id"]}&fs_task_timer=out",
		wrapper=>"vmc_button_time_out",
	);
$check_in_task = array (
		label=>"Check In Task",
		link=>"fs_projects.php?fs_tab=tasks&project_id={$_REQUEST["project_id"]}&fs_tab_option=fs_tasks&fs_action=view&fs_tab_tasks=main&task_id={$_REQUEST["task_id"]}&fs_task_timer=in",
		wrapper=>"vmc_button_time_in",
	);
$complete_task = array (
		label=>"Complete Task",
		link=>"fs_projects.php?fs_tab=tasks&project_id={$_REQUEST["project_id"]}&fs_tab_option=fs_tasks&fs_action=view&fs_tab_tasks=main&task_id={$_REQUEST["task_id"]}&fs_task_timer=complete",
		wrapper=>"vmc_button_complete",
	);
$reopen_task = array (
		label=>"Re-Open Task",
		link=>"fs_projects.php?fs_tab=tasks&project_id={$_REQUEST["project_id"]}&fs_tab_option=fs_tasks&fs_action=view&fs_tab_tasks=main&task_id={$_REQUEST["task_id"]}&fs_task_timer=reopen",
		wrapper=>"vmc_button_reopen",
	);

if ($task_timer_status == "check_complete") {

	$list_options[] = $complete_task;
	if ($task_timer_action == "check_out") {

		$list_options[] = $check_out_task;
	} elseif ($task_timer_action == "check_in") {
		$list_options[] = $check_in_task;
	}
} elseif ($task_timer_status == "check_reopen") {

	$list_options[] = $reopen_task;
}

$_OBJECT_IFACE_LIST = _app_create_list_object (array (
                db_conn=>$_GLOBAL_DBH,
                indexes=>$indexes,
		list_count=>"SELECT COUNT(*) FROM project_task_timer WHERE task_id={$_REQUEST["task_id"]}",
		list_select=>"SELECT project_task_timer.*,
			CONCAT(last,', ',first) AS user_name,
			SEC_TO_TIME(
				CASE date_end
					WHEN '' THEN UNIX_TIMESTAMP()
					ELSE UNIX_TIMESTAMP(date_end)
				END - UNIX_TIMESTAMP(date_begin)
			) AS total_time,
			CASE date_end
				WHEN '' THEN CURRENT_TIMESTAMP()
				ELSE date_end
			END AS date_end
		FROM project_task_timer,users
		WHERE task_id={$_REQUEST["task_id"]}
		AND user_id=users.id",
                list_id=>"lc_project_task_timer",
                list_options=>$list_options,
                primary_key=>"id",
                primary_relation=>"timer_id",
		#list_check_box=>true,
                link_options=>$link_options,
                object_ctl=>$object_ctl
));

$_OBJECT_IFACE_TABBING_PANEL->insert_container ($_OBJECT_IFACE_LIST);

?>
